package cn.com.bluemoon.daps.system.service.impl;

import cn.com.bluemoon.daps.system.entity.DapSysUserRole;
import cn.com.bluemoon.daps.system.mapper.DapSysUserRoleMapper;
import cn.com.bluemoon.daps.system.service.DapSysUserRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户角色关联表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-12-19
 */
@Slf4j
@Service
public class DapSysUserRoleServiceImpl extends ServiceImpl<DapSysUserRoleMapper, DapSysUserRole>
        implements DapSysUserRoleService {

    @Override
    public boolean batchSave(Long userId, List<Long> roleIdList){
        List<DapSysUserRole> userRoleList = roleIdList.stream().map(rId -> {
            DapSysUserRole userRole = new DapSysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(rId);
            return userRole;
        }).collect(Collectors.toList());
        return this.saveBatch(userRoleList);
    }

    @Override
    public boolean deleteByUserId(Long userId){
        LambdaQueryWrapper<DapSysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DapSysUserRole::getUserId,userId);
        return this.remove(wrapper);
    }

    @Override
    public boolean deleteByRoleId(Long roleId){
        LambdaQueryWrapper<DapSysUserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DapSysUserRole::getRoleId,roleId);
        return this.remove(wrapper);
    }
}
